﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ContosoUniversity.Models;

namespace ContosoUniversity.Controllers
{
  public class StudentController : Controller
  {
    private SchoolContext db = new SchoolContext();

    //
    // GET: /Student/

    public ViewResult Index(string sortOrder)
    {
      ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
      ViewBag.FirstNameSortParm = sortOrder == "FirstName" ? "FirstName desc" : "FirstName";
      ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";
      var students = from s in db.Students
                     select s;
      switch (sortOrder)
      { 
        case "Name desc":
          students = students.OrderByDescending(s => s.LastName);
          break;
        case "Date":
          students = students.OrderBy(s => s.EnrollmentDate);
          break;
        case "Date desc":
          students = students.OrderByDescending(s => s.EnrollmentDate);
          break;
        case "FirstName":
          students = students.OrderBy(s => s.FirstMidName);
          break;
        case "FirstName desc":
          students = students.OrderByDescending(s => s.FirstMidName);
          break;
        default:
          students = students.OrderBy(s => s.LastName);
          break;
      }
      return View(students.ToList());
    }

    //
    // GET: /Student/Details/5

    public ViewResult Details(int id)
    {
      Student student = db.Students.Find(id);
      return View(student);
    }

    //
    // GET: /Student/Create

    public ActionResult Create()
    {
      return View();
    }

    //
    // POST: /Student/Create

    [HttpPost]
    public ActionResult Create(Student student)
    {
      try
      {
        if (ModelState.IsValid)
        {
          db.Students.Add(student);
          db.SaveChanges();
          return RedirectToAction("Index");
        }
      }
      catch (DataException)
      {
        // log the error
        ModelState.AddModelError("", "Unable to save changes");
      }
      return View(student);
    }

    //
    // GET: /Student/Edit/5

    public ActionResult Edit(int id)
    {
      Student student = db.Students.Find(id);
      return View(student);
    }

    //
    // POST: /Student/Edit/5

    [HttpPost]
    public ActionResult Edit(Student student)
    {
      try
      {
        if (ModelState.IsValid)
        {
          db.Entry(student).State = EntityState.Modified;
          db.SaveChanges();
          return RedirectToAction("Index");
        }
      }
      catch (DataException)
      {
        // log the error
        ModelState.AddModelError("", "Unable to save changes");
      }

      return View(student);
    }

    //
    // GET: /Student/Delete/5

    public ActionResult Delete(int id, bool? saveChangesError)
    {
      if (saveChangesError.GetValueOrDefault())
      {
        ViewBag.ErrorMessage = "Unable to save changes. Try again, and if the problem persists see your system administrator.";
      }
      Student student = db.Students.Find(id);
      return View(student);
    }

    //
    // POST: /Student/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {
      try
      {
        Student studentToDelete = new Student() { StudentID = id };
        db.Entry(studentToDelete).State = EntityState.Deleted;
        db.SaveChanges();
      }
      catch (DataException)
      {
        //Log the error (add a variable name after DataException)
        return RedirectToAction("Delete",
            new System.Web.Routing.RouteValueDictionary { 
                  { "id", id }, 
                  { "saveChangesError", true } });
      }
      return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
      db.Dispose();
      base.Dispose(disposing);
    }
  }
}